<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-edit"
        @click="handleCreate"
      >
        新建
      </el-button>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 50%;"
    >
      <el-table-column
        label="编号"
        prop="id"
        sortable="custom"
        align="center"
        width="70"
      >
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="名称" prop="name" width="120px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="满分" prop="name" width="120px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.limitScore }}</span>
        </template>
      </el-table-column>
      <el-table-column label="key" prop="name" width="120px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.quaKey }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
          <el-button
            type="primary"
            size="mini"
            @click="handleUpdate(row)"
          >
            编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(row,$index)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="left"
        label-width="90px"
        style="width: 400px; margin-left:50px;"
      >
        <el-form-item label="名称" prop="name">
          <el-col :span="12">
            <el-input v-model="temp.name" />
          </el-col>
        </el-form-item>
        <el-form-item label="满分" prop="name">
          <el-col :span="12">
            <el-input v-model="temp.limitScore" />
          </el-col>
        </el-form-item>
        <el-form-item label="key" prop="name">
          <el-col :span="12">
            <el-input v-model="temp.quaKey" />
          </el-col>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          确定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import {
  deleteQuarantineOption,
  listQuarantineOption,
  patchQuarantineOption,
  postQuarantineOption
} from '@/api/quarantine'

export default {
  name: 'QuarantineOption',

  data() {
    return {
      // 表格数据
      list: [],
      listLoading: false,
      // 表单数据
      temp: {
        id: undefined,
        name: '',
        limitScore: undefined,
        quaKey: ''
      },

      // 固定数据
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '添加'
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      // 改变
      listQuarantineOption().then(response => {
        this.list = response.data
        this.total = response.meta.total
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
      console.log(this.list)
      this.listLoading = false
    },
    // 改变
    resetTemp() {
      this.temp = {
        id: undefined,
        name: '',
        limitScore: undefined,
        quaKey: ''
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    createData() {
      postQuarantineOption(this.temp).then((response) => {
        this.getList()
        this.dialogFormVisible = false
        this.$notify({
          title: '成功',
          message: '添加成功!',
          type: 'success',
          duration: 2000
        })
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
    },
    updateData() {
      patchQuarantineOption(this.temp).then(() => {
        this.getList()
        this.dialogFormVisible = false
        this.$notify({
          title: '成功',
          message: '更新成功!',
          type: 'success',
          duration: 2000
        })
      })
    },
    handleDelete(row, index) {
      this.$confirm('是否删除该数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteQuarantineOption(row.id).then(() => {
          this.getList()
          this.$notify({
            title: '成功',
            message: '删除成功!',
            type: 'success',
            duration: 2000
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style scoped>

</style>
